package com.campus.controller;

import com.campus.pojo.dto.HomeworkDTO;
import com.campus.pojo.dto.HomeworkPageQueryDTO;
import com.campus.pojo.entity.Homework;
import com.campus.pojo.vo.HomeworkVO;
import com.campus.result.PageResult;
import com.campus.result.Result;
import com.campus.service.HomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/homework")
@Slf4j
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;

    /**
     * 分页查询
     */
    @GetMapping("/pageQuery")
    public Result<PageResult<HomeworkVO>> pageQuery(HomeworkPageQueryDTO homeworkPageQueryDTO) {
        log.info("分页查询：{}", homeworkPageQueryDTO);
        PageResult<HomeworkVO> pageResult = homeworkService.pageQuery(homeworkPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除 集合接收ids
     */
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除作业：{}", ids);
        homeworkService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 添加作业
     */
    @PostMapping
    public Result save(@RequestBody HomeworkDTO homeworkDTO) {
        log.info("添加作业：{}", homeworkDTO);
        homeworkService.save(homeworkDTO);
        return Result.success();
    }

    /**
     * 修改作业状态
     */
    @PutMapping("/status/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("修改作业状态为：{}", status == 0 ? "已发布" : "已截止");
        homeworkService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 修改作业
     */
    @PutMapping
    public Result update(@RequestBody HomeworkDTO homeworkDTO) {
        log.info("修改作业：{}", homeworkDTO);
        homeworkService.update(homeworkDTO);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result<HomeworkVO> getById(@PathVariable Long id) {
        log.info("查询回显 id: {}", id);
        HomeworkVO homeworkVO = homeworkService.getById(id);
        return Result.success(homeworkVO);
    }




}
